<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="聚合什么是聚合管道？  聚合管道是基于数据处理管道概念建模的数据聚合框架。 文档进入多阶段管道，将文档转换为聚合结果。 MongoDB 聚合管道由阶段组成。每个阶段都会在文档通过管道时对其进行转换。 管道阶段可以在管道中多次出现，但$out，$merge、 和 $geoNear阶段除外。（相当于 Java 流式计算的的最后一步归约操作）。  语法 1db.collection.aggregate(">
<meta property="og:type" content="article">
<meta property="og:title" content="MongoDB 进阶(聚合)">
<meta property="og:url" content="http://example.com/2021/10/19/MongoDB-%E8%BF%9B%E9%98%B6(%E8%81%9A%E5%90%88)/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="聚合什么是聚合管道？  聚合管道是基于数据处理管道概念建模的数据聚合框架。 文档进入多阶段管道，将文档转换为聚合结果。 MongoDB 聚合管道由阶段组成。每个阶段都会在文档通过管道时对其进行转换。 管道阶段可以在管道中多次出现，但$out，$merge、 和 $geoNear阶段除外。（相当于 Java 流式计算的的最后一步归约操作）。  语法 1db.collection.aggregate(">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-19T00:57:21.000Z">
<meta property="article:modified_time" content="2021-12-29T19:03:36.379Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="NoSQL">
<meta property="article:tag" content="MongoDB">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/10/19/MongoDB-%E8%BF%9B%E9%98%B6(%E8%81%9A%E5%90%88)/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/10/19/MongoDB-%E8%BF%9B%E9%98%B6(%E8%81%9A%E5%90%88)/","path":"2021/10/19/MongoDB-进阶(聚合)/","title":"MongoDB 进阶(聚合)"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>MongoDB 进阶(聚合) | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%81%9A%E5%90%88"><span class="nav-number">1.</span> <span class="nav-text">聚合</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%81%9A%E5%90%88%E7%AE%A1%E9%81%93%E7%9A%84%E9%98%B6%E6%AE%B5"><span class="nav-number">1.1.</span> <span class="nav-text">聚合管道的阶段</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%81%9A%E5%90%88%E7%AE%A1%E9%81%93%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">1.2.</span> <span class="nav-text">聚合管道表达式</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E4%BE%8B%E5%AD%90"><span class="nav-number">2.</span> <span class="nav-text">常用例子</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#group"><span class="nav-number">2.1.</span> <span class="nav-text">group</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AB%E4%B9%89%E4%B8%8E%E4%BD%9C%E7%94%A8"><span class="nav-number">2.1.1.</span> <span class="nav-text">含义与作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95"><span class="nav-number">2.1.2.</span> <span class="nav-text">语法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E4%BE%8B%E5%AD%90"><span class="nav-number">2.1.3.</span> <span class="nav-text">小例子</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lookup%E5%AE%9E%E7%8E%B0%E5%85%B3%E8%81%94%E6%9F%A5%E8%AF%A2"><span class="nav-number">2.2.</span> <span class="nav-text">lookup实现关联查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AB%E4%B9%89%E4%B8%8E%E4%BD%9C%E7%94%A8-1"><span class="nav-number">2.2.1.</span> <span class="nav-text">含义与作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E6%B3%95-1"><span class="nav-number">2.2.2.</span> <span class="nav-text">语法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E4%BE%8B%E5%AD%90-1"><span class="nav-number">2.2.3.</span> <span class="nav-text">小例子</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/10/19/MongoDB-%E8%BF%9B%E9%98%B6(%E8%81%9A%E5%90%88)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="MongoDB 进阶(聚合) | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MongoDB 进阶(聚合)
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-19 08:57:21" itemprop="dateCreated datePublished" datetime="2021-10-19T08:57:21+08:00">2021-10-19</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2021-12-30 03:03:36" itemprop="dateModified" datetime="2021-12-30T03:03:36+08:00">2021-12-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/NoSQL/" itemprop="url" rel="index"><span itemprop="name">NoSQL</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="聚合"><a href="#聚合" class="headerlink" title="聚合"></a>聚合</h1><p>什么是聚合管道？</p>
<ul>
<li><strong>聚合管道是基于数据处理管道概念建模的数据聚合框架</strong>。</li>
<li>文档进入<strong>多阶段管道</strong>，将文档转换为聚合结果。</li>
<li>MongoDB 聚合管道由<strong>阶段</strong>组成。每个阶段都会在文档通过管道时对其进行转换。</li>
<li>管道阶段可以在管道中多次出现，但<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out"><code>$out</code></a>，<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge"><code>$merge</code></a>、 和 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/#mongodb-pipeline-pipe.-geoNear"><code>$geoNear</code></a>阶段除外。（相当于 Java 流式计算的的最后一步归约操作）。</li>
</ul>
<p><strong>语法</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection.aggregate( [ &#123; &lt;stage&gt; &#125;, ... ] )</span><br></pre></td></tr></table></figure>



<h2 id="聚合管道的阶段"><a href="#聚合管道的阶段" class="headerlink" title="聚合管道的阶段"></a>聚合管道的阶段</h2><p>这里只列出最常用的，完整请看参考文档。</p>
<table>
<thead>
<tr>
<th align="left">阶段</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields"><code>$addFields</code></a></td>
<td align="left">向文档添加新字段。类似于 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project"><code>$project</code></a>，<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields"><code>$addFields</code></a>对流中的每个文档进行整形；具体来说，通过向包含输入文档中现有字段和新添加字段的输出文档添加新字段。<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set"><code>$set</code></a>是 的别名<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields"><code>$addFields</code></a>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/count/#mongodb-pipeline-pipe.-count"><code>$count</code></a></td>
<td align="left">返回聚合管道此阶段的文档数计数。与<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/count-accumulator/#mongodb-group-grp.-count"><code>$count</code></a>聚合累加器不同。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group"><code>$group</code></a></td>
<td align="left">按指定的标识符表达式对输入文档进行分组，并将累加器表达式（如果指定）应用于每个组。使用所有输入文档并为每个不同的组输出一个文档。输出文档仅包含标识符字段和累积字段（如果指定）。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/limit/#mongodb-pipeline-pipe.-limit"><code>$limit</code></a></td>
<td align="left">将未修改的前<em>n 个</em>文档传递到管道，其中<em>n</em>是指定的限制。对于每个输入文档，输出一个文档（对于前<em>n 个</em>文档）或零个文档（在前<em>n 个</em>文档之后）。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup"><code>$lookup</code></a></td>
<td align="left">对<em>同一</em>数据库中的另一个集合执行左外部 联接，以从“联接”集合中过滤文档以进行处理。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match"><code>$match</code></a></td>
<td align="left">过滤文档流以只允许匹配的文档未经修改地传递到下一个管道阶段。 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match"><code>$match</code></a>使用标准的 MongoDB 查询。对于每个输入文档，输出一个文档（匹配）或零个文档（不匹配）。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge"><code>$merge</code></a></td>
<td align="left">将聚合管道的结果文档写入集合。该阶段可以将（插入新文档、合并文档、替换文档、保留现有文档、操作失败、使用自定义更新管道处理文档）结果合并到输出集合中。要使用该<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge"><code>$merge</code></a>阶段，它必须是管道中的最后一个阶段。<em>4.2版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out"><code>$out</code></a></td>
<td align="left">将聚合管道的结果文档写入集合。要使用该<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out"><code>$out</code></a>阶段，它必须是管道中的最后一个阶段。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/planCacheStats/#mongodb-pipeline-pipe.-planCacheStats"><code>$planCacheStats</code></a></td>
<td align="left">返回集合的<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/core/query-plans/">计划缓存</a>信息。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project"><code>$project</code></a></td>
<td align="left">重塑流中的每个文档，例如通过添加新字段或删除现有字段。对于每个输入文档，输出一个文档。另请参阅<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset"><code>$unset</code></a>删除现有字段。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith"><code>$replaceWith</code></a></td>
<td align="left">用指定的嵌入文档替换文档。该操作替换输入文档中的所有现有字段，包括该<code>_id</code>字段。指定嵌入在输入文档中的文档以将嵌入的文档提升到顶级。<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/replaceWith/#mongodb-pipeline-pipe.-replaceWith"><code>$replaceWith</code></a>是<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot"><code>$replaceRoot</code></a>stage的别名 。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/sample/#mongodb-pipeline-pipe.-sample"><code>$sample</code></a></td>
<td align="left">从其输入中随机选择指定数量的文档。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.atlas.mongodb.com/reference/atlas-search/query-syntax/#mongodb-pipeline-pipe.-search"><code>$search</code></a></td>
<td align="left">对<a target="_blank" rel="noopener" href="https://docs.atlas.mongodb.com/reference/atlas-search/query-syntax/">Atlas</a> 集合中的一个或多个字段执行全文搜索 。笔记<code>$search</code> 仅适用于 MongoDB Atlas 集群，不适用于自管理部署。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set"><code>$set</code></a></td>
<td align="left">向文档添加新字段。类似于 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project"><code>$project</code></a>，<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set"><code>$set</code></a>对流中的每个文档进行整形；具体来说，通过向包含输入文档中现有字段和新添加字段的输出文档添加新字段。<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set"><code>$set</code></a>是<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields"><code>$addFields</code></a>stage的别名。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/skip/#mongodb-pipeline-pipe.-skip"><code>$skip</code></a></td>
<td align="left">跳过前<em>n 个</em>文档，其中<em>n</em>是指定的跳过编号，并将未修改的剩余文档传递到管道。对于每个输入文档，输出零个文档（对于前<em>n 个</em>文档）或一个文档（如果在前<em>n 个</em>文档之后）。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort"><code>$sort</code></a></td>
<td align="left">按指定的排序键对文档流重新排序。只是顺序变了；文件保持不变。对于每个输入文档，输出一个文档。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/#mongodb-pipeline-pipe.-unionWith"><code>$unionWith</code></a></td>
<td align="left">执行两个集合的并集；ie 将来自两个集合的管道结果合并为一个结果集。<em>4.4版中的新功能</em>。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset"><code>$unset</code></a></td>
<td align="left">从文档中删除&#x2F;排除字段。<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset"><code>$unset</code></a>是<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project"><code>$project</code></a>删除字段的阶段的别名。</td>
</tr>
</tbody></table>
<p><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/">参考文档</a></p>
<h2 id="聚合管道表达式"><a href="#聚合管道表达式" class="headerlink" title="聚合管道表达式"></a>聚合管道表达式</h2><blockquote>
<p>详细的 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/">参考文档</a></p>
</blockquote>
<h1 id="常用例子"><a href="#常用例子" class="headerlink" title="常用例子"></a>常用例子</h1><h2 id="group"><a href="#group" class="headerlink" title="group"></a>group</h2><h3 id="含义与作用"><a href="#含义与作用" class="headerlink" title="含义与作用"></a>含义与作用</h3><p>按指定的<code>_id</code>表达式对输入文档进行分组，并为每个不同的分组输出一个文档。<code>_id</code>每个输出文档的字段都包含唯一的按值分组。输出文档还可以包含保存某些<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/#std-label-accumulators-group">累加器表达式</a>值的计算字段。</p>
<h3 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  $group:</span><br><span class="line">    &#123;</span><br><span class="line">      _id: &lt;expression&gt;, // Group By Expression</span><br><span class="line">      &lt;field1&gt;: &#123; &lt;accumulator1&gt; : &lt;expression1&gt; &#125;,</span><br><span class="line">      ...</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>



<table>
<thead>
<tr>
<th align="left">Field</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><code>_id</code></td>
<td align="left"><em>必须的</em>。如果指定null或者常量，相当于不分组。</td>
</tr>
<tr>
<td align="left"><code>field</code></td>
<td align="left"><em>可选的.</em> 使用 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/#std-label-accumulators-group">accumulator operators</a>.计算</td>
</tr>
</tbody></table>
<p>常用的 <strong>累加器运算符</strong>：</p>
<table>
<thead>
<tr>
<th align="left">名称</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg"><code>$avg</code></a></td>
<td align="left">返回数值的平均值。忽略非数字值。<em>在5.0版更改</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/count-accumulator/#mongodb-group-grp.-count"><code>$count</code></a></td>
<td align="left">返回组中的文档数。区别于<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/count/#mongodb-pipeline-pipe.-count"><code>$count</code></a>流水线阶段。<em>5.0版中的新功能</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group"><code>$group</code></a>和 <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/first/#mongodb-group-grp.-first"><code>$first</code></a></td>
<td align="left">从每个组的第一个文档返回一个值。仅当文档已排序时才定义顺序。与<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/first-array-element/#mongodb-expression-exp.-first"><code>$first</code></a>数组运算符不同。<em>在5.0版更改</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/last/#mongodb-group-grp.-last"><code>$last</code></a></td>
<td align="left">从每个组的最后一个文档返回一个值。仅当文档已排序时才定义顺序。与<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/last-array-element/#mongodb-expression-exp.-last"><code>$last</code></a>数组运算符不同。<em>在5.0版更改</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/max/#mongodb-group-grp.-max"><code>$max</code></a></td>
<td align="left">返回每个组的最高表达式值。<em>在5.0版更改</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/min/#mongodb-group-grp.-min"><code>$min</code></a></td>
<td align="left">返回每个组的最低表达式值。<em>在5.0版更改</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum"><code>$sum</code></a></td>
<td align="left">返回数值的总和。忽略非数字值。<em>在5.0版更改</em>：在<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields"><code>$setWindowFields</code></a>阶段可用。</td>
</tr>
</tbody></table>
<h3 id="小例子"><a href="#小例子" class="headerlink" title="小例子"></a>小例子</h3><ol>
<li>以下聚合操作按<code>item</code> 字段对文档进行分组，计算每个商品的总销售额并仅返回总销售额大于或等于 100 的商品：</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">db.sales.aggregate(</span><br><span class="line">  [</span><br><span class="line">    // First Stage</span><br><span class="line">    &#123;</span><br><span class="line">      $group :</span><br><span class="line">        &#123;</span><br><span class="line">          _id : &quot;$item&quot;,</span><br><span class="line">          totalSaleAmount: &#123; $sum: &#123; $multiply: [ &quot;$price&quot;, &quot;$quantity&quot; ] &#125; &#125;</span><br><span class="line">        &#125;</span><br><span class="line">     &#125;,</span><br><span class="line">     // Second Stage</span><br><span class="line">     &#123;</span><br><span class="line">       $match: &#123; &quot;totalSaleAmount&quot;: &#123; $gte: 100 &#125; &#125;</span><br><span class="line">     &#125;</span><br><span class="line">   ]</span><br><span class="line"> )</span><br></pre></td></tr></table></figure>



<ol start="2">
<li>计算计数、总和和平均值</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">db.sales.aggregate([</span><br><span class="line">  // First Stage</span><br><span class="line">  &#123;</span><br><span class="line">    $match : &#123; &quot;date&quot;: &#123; $gte: new ISODate(&quot;2014-01-01&quot;), $lt: new ISODate(&quot;2015-01-01&quot;) &#125; &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  // Second Stage</span><br><span class="line">  &#123;</span><br><span class="line">    $group : &#123;</span><br><span class="line">       _id : &#123; $dateToString: &#123; format: &quot;%Y-%m-%d&quot;, date: &quot;$date&quot; &#125; &#125;,</span><br><span class="line">       totalSaleAmount: &#123; $sum: &#123; $multiply: [ &quot;$price&quot;, &quot;$quantity&quot; ] &#125; &#125;,</span><br><span class="line">       averageQuantity: &#123; $avg: &quot;$quantity&quot; &#125;,</span><br><span class="line">       count: &#123; $sum: 1 &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  // Third Stage</span><br><span class="line">  &#123;</span><br><span class="line">    $sort : &#123; totalSaleAmount: -1 &#125;</span><br><span class="line">  &#125;</span><br><span class="line"> ])</span><br></pre></td></tr></table></figure>

<ul>
<li><p>第一阶段：</p>
<p>该<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match"><code>$match</code></a>阶段对文档进行过滤，仅将 2014 年的文档传递到下一阶段。</p>
</li>
<li><p>第二阶段：</p>
<p>该<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group"><code>$group</code></a>阶段按日期对单据进行分组，并计算每组单据的总销售额、平均数量和总数。</p>
</li>
<li><p>第三阶段：</p>
<p>该<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort"><code>$sort</code></a>阶段按每个组的总销售额以降序对结果进行排序。</p>
</li>
</ul>
<p>相当于如下SQL</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="type">date</span>,</span><br><span class="line">       <span class="built_in">Sum</span>(( price <span class="operator">*</span> quantity )) <span class="keyword">AS</span> totalSaleAmount,</span><br><span class="line">       <span class="built_in">Avg</span>(quantity)             <span class="keyword">AS</span> averageQuantity,</span><br><span class="line">       <span class="built_in">Count</span>(<span class="operator">*</span>)                  <span class="keyword">AS</span> Count</span><br><span class="line"><span class="keyword">FROM</span>   sales</span><br><span class="line"><span class="keyword">GROUP</span>  <span class="keyword">BY</span> <span class="type">Date</span>(<span class="type">date</span>)</span><br><span class="line"><span class="keyword">ORDER</span>  <span class="keyword">BY</span> totalSaleAmount <span class="keyword">DESC</span></span><br></pre></td></tr></table></figure>







<h2 id="lookup实现关联查询"><a href="#lookup实现关联查询" class="headerlink" title="lookup实现关联查询"></a>lookup实现关联查询</h2><blockquote>
<p>参考<a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/">官方文档</a></p>
</blockquote>
<h3 id="含义与作用-1"><a href="#含义与作用-1" class="headerlink" title="含义与作用"></a>含义与作用</h3><p>对<em>同一</em> 数据库中的未分片集合执行左外部联接，以从“联接”集合中过滤文档以进行处理。对于每个输入文档，lookup阶段会添加一个新的数组字段，其元素是“joined”集合中的匹配文档。该lookup阶段将这些重塑的文档传递到下一阶段。</p>
<h3 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line"><span class="meta prompt_">   $</span><span class="language-bash">lookup:</span></span><br><span class="line">     &#123;</span><br><span class="line">       from: &lt;collection to join&gt;,</span><br><span class="line">       localField: &lt;field from the input documents&gt;,</span><br><span class="line">       foreignField: &lt;field from the documents of the &quot;from&quot; collection&gt;,</span><br><span class="line">       as: &lt;output array field&gt;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>具体的字段的含义是：</p>
<table>
<thead>
<tr>
<th align="left">Field</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody><tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-lookup-eq-from">from</a></td>
<td align="left">Specifies the collection in the <em>same</em> database to perform the join with. The <code>from</code> collection cannot be sharded. For details, see <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-lookup-sharded-collections">Sharded Collection Restrictions</a>.</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-lookup-eq-localField">localField</a></td>
<td align="left">Specifies the field from the documents input to the <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup"><code>$lookup</code></a> stage. <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup"><code>$lookup</code></a> performs an equality match on the <code>localField</code> to the <code>foreignField</code> from the documents of the <code>from</code> collection. If an input document does not contain the <code>localField</code>, the <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup"><code>$lookup</code></a> treats the field as having a value of <code>null</code> for matching purposes.</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-lookup-eq-foreignField">foreignField</a></td>
<td align="left">Specifies the field from the documents in the <code>from</code> collection. <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup"><code>$lookup</code></a> performs an equality match on the <code>foreignField</code> to the <code>localField</code> from the input documents. If a document in the <code>from</code> collection does not contain the <code>foreignField</code>, the <a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup"><code>$lookup</code></a> treats the value as <code>null</code> for matching purposes.</td>
</tr>
<tr>
<td align="left"><a target="_blank" rel="noopener" href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#std-label-lookup-eq-as">as</a></td>
<td align="left">Specifies the name of the new array field to add to the input documents. The new array field contains the matching documents from the <code>from</code> collection. If the specified name already exists in the input document, the existing field is <em>overwritten</em>.</td>
</tr>
</tbody></table>
<p>上面的查询语句等效为：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span>, <span class="operator">&lt;</span>output <span class="keyword">array</span> field<span class="operator">&gt;</span></span><br><span class="line"><span class="keyword">FROM</span> collection</span><br><span class="line"><span class="keyword">WHERE</span> <span class="operator">&lt;</span>output <span class="keyword">array</span> field<span class="operator">&gt;</span> <span class="keyword">IN</span> (</span><br><span class="line">   <span class="keyword">SELECT</span> <span class="operator">*</span></span><br><span class="line">   <span class="keyword">FROM</span> <span class="operator">&lt;</span>collection <span class="keyword">to</span> <span class="keyword">join</span><span class="operator">&gt;</span></span><br><span class="line">   <span class="keyword">WHERE</span> <span class="operator">&lt;</span>foreignField<span class="operator">&gt;</span> <span class="operator">=</span> <span class="operator">&lt;</span>collection.localField<span class="operator">&gt;</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h3 id="小例子-1"><a href="#小例子-1" class="headerlink" title="小例子"></a>小例子</h3><p>订单表</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">db.orders.insertMany( [</span><br><span class="line">   &#123; &quot;_id&quot; : 1, &quot;item&quot; : &quot;almonds&quot;, &quot;price&quot; : 12, &quot;quantity&quot; : 2 &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 2, &quot;item&quot; : &quot;pecans&quot;, &quot;price&quot; : 20, &quot;quantity&quot; : 1 &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 3  &#125;</span><br><span class="line">] )</span><br></pre></td></tr></table></figure>



<p>库存表</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">db.inventory.insertMany( [</span><br><span class="line">   &#123; &quot;_id&quot; : 1, &quot;sku&quot; : &quot;almonds&quot;, &quot;description&quot;: &quot;product 1&quot;, &quot;instock&quot; : 120 &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 2, &quot;sku&quot; : &quot;bread&quot;, &quot;description&quot;: &quot;product 2&quot;, &quot;instock&quot; : 80 &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 3, &quot;sku&quot; : &quot;cashews&quot;, &quot;description&quot;: &quot;product 3&quot;, &quot;instock&quot; : 60 &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 4, &quot;sku&quot; : &quot;pecans&quot;, &quot;description&quot;: &quot;product 4&quot;, &quot;instock&quot; : 70 &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 5, &quot;sku&quot;: null, &quot;description&quot;: &quot;Incomplete&quot; &#125;,</span><br><span class="line">   &#123; &quot;_id&quot; : 6 &#125;</span><br><span class="line">] )</span><br></pre></td></tr></table></figure>



<p>查询语句</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">db.orders.aggregate( [</span><br><span class="line">   &#123;</span><br><span class="line">     $lookup:</span><br><span class="line">       &#123;</span><br><span class="line">         from: &quot;inventory&quot;,</span><br><span class="line">         localField: &quot;item&quot;,</span><br><span class="line">         foreignField: &quot;sku&quot;,</span><br><span class="line">         as: &quot;inventory_docs&quot;</span><br><span class="line">       &#125;</span><br><span class="line">  &#125;</span><br><span class="line">] )</span><br></pre></td></tr></table></figure>



<p>查询结果</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">   <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;item&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;almonds&quot;</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;price&quot;</span> <span class="punctuation">:</span> <span class="number">12</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;quantity&quot;</span> <span class="punctuation">:</span> <span class="number">2</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;inventory_docs&quot;</span> <span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line">      <span class="punctuation">&#123;</span> <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span> <span class="attr">&quot;sku&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;almonds&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;description&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;product 1&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;instock&quot;</span> <span class="punctuation">:</span> <span class="number">120</span> <span class="punctuation">&#125;</span></span><br><span class="line">   <span class="punctuation">]</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">   <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">2</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;item&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;pecans&quot;</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;price&quot;</span> <span class="punctuation">:</span> <span class="number">20</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;quantity&quot;</span> <span class="punctuation">:</span> <span class="number">1</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;inventory_docs&quot;</span> <span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line">      <span class="punctuation">&#123;</span> <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">4</span><span class="punctuation">,</span> <span class="attr">&quot;sku&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;pecans&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;description&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;product 4&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;instock&quot;</span> <span class="punctuation">:</span> <span class="number">70</span> <span class="punctuation">&#125;</span></span><br><span class="line">   <span class="punctuation">]</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">   <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">3</span><span class="punctuation">,</span></span><br><span class="line">   <span class="attr">&quot;inventory_docs&quot;</span> <span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line">      <span class="punctuation">&#123;</span> <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">5</span><span class="punctuation">,</span> <span class="attr">&quot;sku&quot;</span> <span class="punctuation">:</span> <span class="literal"><span class="keyword">null</span></span><span class="punctuation">,</span> <span class="attr">&quot;description&quot;</span> <span class="punctuation">:</span> <span class="string">&quot;Incomplete&quot;</span> <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">      <span class="punctuation">&#123;</span> <span class="attr">&quot;_id&quot;</span> <span class="punctuation">:</span> <span class="number">6</span> <span class="punctuation">&#125;</span></span><br><span class="line">   <span class="punctuation">]</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>








    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/NoSQL/" rel="tag"># NoSQL</a>
              <a href="/tags/MongoDB/" rel="tag"># MongoDB</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/17/Linux%E8%BF%9B%E7%A8%8B%E8%B0%83%E5%BA%A6%E9%82%A3%E4%BA%9B%E4%BA%8B/" rel="prev" title="Linux进程调度那些事">
                  <i class="fa fa-chevron-left"></i> Linux进程调度那些事
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/19/ZooKeeper%E5%85%A5%E9%97%A8/" rel="next" title="ZooKeeper入门">
                  ZooKeeper入门 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
